//
//  GoodsDetailsHeaderView.m
//  Findif
//
//  Created by Cicel Chen on 2018/10/22.
//  Copyright © 2018年 Cicel Chen. All rights reserved.
//

#import "GoodsDetailsHeaderView.h"
#import "XHStarRateView.h"
#import <CoreLocation/CoreLocation.h>
#import "AlreadyCollectionViewCell.h"
#import "ZYBannerView.h"

@interface GoodsDetailsHeaderView ()<UICollectionViewDelegate,UICollectionViewDataSource,ZYBannerViewDataSource, ZYBannerViewDelegate>

@property (nonatomic, strong) ZYBannerView *banner;
@property (nonatomic, strong)NSArray * dataArray;
@property (nonatomic, weak)UIImageView * topImageView;
@property (nonatomic, weak)UILabel * shopLabel;
@property (nonatomic, weak)UILabel * distanceLabel;
@property (nonatomic, weak)XHStarRateView * starView;
@property (nonatomic, weak)UILabel * commentLabel;
@property (nonatomic, strong)UILabel * timeLabel;
@property (nonatomic, strong)UILabel * localLabel;
@property (nonatomic, strong)UIImageView * timeImageView;
@property (nonatomic, strong)UIImageView * localImageView;
@property (nonatomic, strong)UIButton * callButton;
@property (nonatomic, strong)UIButton * msgButton;
@property (nonatomic, strong)UIView * oneLineView;
@property (nonatomic, strong)UIView * twoLineView;
@property (nonatomic, strong)UIImageView * couponImageView;
@property (nonatomic, strong)UILabel * pointsLabel;
//@property (nonatomic, weak)UILabel * redLabel;
@property (nonatomic, strong)UIButton * pointsDetailsButton;
@property (nonatomic, strong)UIButton * joinedDetailsButton;
@property (nonatomic, strong)UIButton * commitDetailsButton;
@property (nonatomic, strong)UILabel * validDateLabel;
@property (nonatomic, strong)UIView * threeLineView;
@property (nonatomic, strong)UIImageView * joinImageView;
@property (nonatomic, strong)UILabel * joinLabel;
@property (nonatomic, strong)UIImageView * commmitImageView;
@property (nonatomic, strong)UILabel * titleCommitLabel;
@property (nonatomic, strong)UIView * fourLineView;
@property (nonatomic, strong)UIView * joinView;

@property (nonatomic, weak)UILabel * noDataLabel;
@property (nonatomic, weak)UICollectionView * collectionView;
@property (nonatomic, strong)UICollectionViewFlowLayout *layout;
@property (nonatomic, strong)NSArray * userArray;
@property (nonatomic, copy)NSString * merchants_user_id;

@end

@implementation GoodsDetailsHeaderView

- (instancetype)initWithFrame:(CGRect)frame{
    
    if ([super initWithFrame:frame]) {
        
        [self setupUI];
    }
    
    return self;
}


- (void)setDict:(NSDictionary *)dict{
    
    _dict = dict;
//    merchants_goods_img
//    NSLog(@"dict===%@",dict);
//    NSArray *array = [dict[@"merchants_goods_img"] componentsSeparatedByString:@","];
//    NSString * img = array.firstObject;
//    NSString * usrlStr = [NSString stringWithFormat:@"%@%@",BASEURLSTR,img];
//    NSURL * url = [NSURL URLWithString:usrlStr];
//    [self.topImageView sd_setImageWithURL:url];
    self.dataArray = [dict[@"merchants_goods_img"] componentsSeparatedByString:@","];
    self.shopLabel.text = dict[@"merchants_goods_name"];
    CGFloat height = [self cellHeightWithMsg:self.shopLabel.text WithLabel:self.shopLabel];
//    NSLog(@"height ==%f",height);
    if (height > 24) {
        self.starView.y = 304 * SCREENH6;
    }
    int count = [dict[@"merchants_goods_comments_star_level"] intValue];
    if (count == 0) {
        
        self.starView.width = 1 * SCREENH6;
    }else{
        
       self.starView.currentRating = count;
    }
    
    self.commentLabel.text = [NSString stringWithFormat:@"%@条评论",dict[@"merchants_goods_comments_number"]];
    self.localLabel.text = dict[@"merchants_address"];
//    int merchants_goods_type = [dict[@"merchants_goods_type"] intValue];
//    if (merchants_goods_type == 1) {
    
      self.timeLabel.text = dict[@"merchants_goods_available_time"];
//    }else{
        
//      self.timeLabel.text = dict[@"merchants_goods_available_time"];
//    }

    self.merchants_user_id = [NSString stringWithFormat:@"%@",dict[@"merchants_user_id"]];
    double lat = [dict[@"merchants_latitude"] doubleValue];
    double lon = [dict[@"merchants_longitude"] doubleValue];
    
    double userLat = [DEF_GET_OBJECT(@"lat") doubleValue];
    double userLon = [DEF_GET_OBJECT(@"lon") doubleValue];
    
    
    double res = [self distanceBetweenOrderBy:lat :userLat :lon :userLon];
    if (res > 1000) {
        
        res = res / 1000;
        self.distanceLabel.text = [NSString stringWithFormat:@"%.2fkm",res];
    }else{
        
        self.distanceLabel.text = [NSString stringWithFormat:@"%.2fm",res];
    }

    self.validDateLabel.text = [NSString stringWithFormat:@"截止时间:%@",dict[@"merchants_goods_end_time"]];
    
    self.userArray = dict[@"AlreadyUser"];
    self.joinLabel.text = [NSString stringWithFormat:@"%lu位用户参加",self.userArray.count];
    
    if (self.userArray.count > 0) {
        
        self.noDataLabel.hidden = YES;
    }else{
        
        self.noDataLabel.hidden = NO;
    }
}

- (void)setDataArray:(NSArray *)dataArray{
    
    _dataArray = dataArray;
    
    [self.banner reloadData];
}


- (void)setUserArray:(NSArray *)userArray{
    
    _userArray = userArray;
  
    [self.collectionView reloadData];
}


- (void)setupUI{
    
    self.backgroundColor = ZRWhiteColor;
    self.userArray = [NSArray array];
    
    // 配置banner
    [self setupBanner];
    
//    UIImageView * topImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,kScreenW,240 * SCREENH6)];
//
//    topImageView.backgroundColor = [UIColor lightGrayColor];
//
//    self.topImageView = topImageView;
//
//    [self addSubview:self.topImageView];
    
    UILabel * shopLabel = [UILabel new];
    
    shopLabel.textColor = ZRBlackColor;
    shopLabel.text = @"王者餐厅";
    shopLabel.font = [UIFont boldSystemFontOfSize:20 * SCREENH6];
    shopLabel.numberOfLines = 2;
    
    self.shopLabel = shopLabel;
    
    [self addSubview:self.shopLabel];
    
    UILabel * distanceLabel = [UILabel new];
    
    distanceLabel.text = @"170m";
    distanceLabel.textColor = [UIColor lightGrayColor];
    distanceLabel.font = [UIFont systemFontOfSize:15 * SCREENH6];
    distanceLabel.textAlignment = NSTextAlignmentRight;
    
    self.distanceLabel = distanceLabel;
    
    [self addSubview:self.distanceLabel];
    
    
    XHStarRateView *starRateView = [[XHStarRateView alloc] initWithFrame:CGRectMake(15 * SCREENH6, 280 * SCREENH6, 70 * SCREENH6, 10 * SCREENH6)];
    starRateView.isAnimation = YES; // 有动画
    starRateView.rateStyle = XHStarRateViewRateStyeFullStar; //允许不完整星评论
    //    starRateView.delegate = self;
    starRateView.currentRating = 5;
    starRateView.userInteractionEnabled = NO;
    
    self.starView = starRateView;
    [self addSubview:self.starView];
    
    
    UILabel * commentLabel = [UILabel new];
    
    commentLabel.text = @"140条评论";
    commentLabel.textColor = [UIColor lightGrayColor];
    commentLabel.font = [UIFont systemFontOfSize:15 * SCREENH6];
    
    self.commentLabel = commentLabel;
    
    [self addSubview:self.commentLabel];
    
    
    self.timeImageView = [self setupImageViewImageWithImage:@"clock-circular-outline"];
    [self addSubview:self.timeImageView];
    self.localImageView = [self setupImageViewImageWithImage:@"place(1)"];
    [self addSubview:self.localImageView];
    self.couponImageView = [self setupImageViewImageWithImage:@"coupon(1)"];
    [self addSubview:self.couponImageView];
    
    self.timeLabel = [self setupLabelWithTitle:@"12:00 AM - 21:00 PM"];
    self.timeLabel.numberOfLines = 0;
    [self addSubview:self.timeLabel];
    self.localLabel = [self setupLabelWithTitle:@"成都剑兰大道29号"];
    self.localLabel.numberOfLines = 0;
    UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction)];
    self.localLabel.textColor = ColorWithRGB(0x4c87d8);
    self.localLabel.userInteractionEnabled = YES;
    [self.localLabel addGestureRecognizer:tapGR];
    [self addSubview:self.localLabel];
    self.pointsLabel = [self setupLabelWithTitle:@"优惠券"];
    [self addSubview:self.pointsLabel];
    
    self.callButton = [self setupButtonWithImage:@"phone-receiver"];
    [self addSubview:self.callButton];
    self.msgButton = [self setupButtonWithImage:@"support"];
    [self addSubview:self.msgButton];
    
    self.oneLineView = [self setupLineView];
    [self addSubview:self.oneLineView];
    self.twoLineView = [self setupLineView];
    [self addSubview:self.twoLineView];
    self.threeLineView = [self setupLineView];
    [self addSubview:self.threeLineView];
    self.fourLineView = [self setupLineView];
    [self addSubview:self.fourLineView];
    
    self.pointsDetailsButton = [self setupMoreButtonWithTitle:@"查看详情"];
    [self addSubview:self.pointsDetailsButton];
//    self.joinedDetailsButton = [self setupMoreButtonWithTitle:@"查看详情"];
//    [self addSubview:self.joinedDetailsButton];
    self.commitDetailsButton = [self setupMoreButtonWithTitle:@"查看更多"];
    [self addSubview:self.commitDetailsButton];
    
    self.validDateLabel = [self setupLabelWithTitle:@"可用时间:9月21日 - 10月20日"];
    [self addSubview:self.validDateLabel];
    self.joinLabel = [self setupLabelWithTitle:@"10位用户参加"];
    [self addSubview:self.joinLabel];
    self.titleCommitLabel = [self setupLabelWithTitle:@"评论"];
    [self addSubview:self.titleCommitLabel];
    
    self.joinImageView = [self setupImageViewImageWithImage:@"VectorSmartObject"];
    [self addSubview:self.joinImageView];
    self.commmitImageView = [self setupImageViewImageWithImage:@"图标"];
    [self addSubview:self.commmitImageView];

    
//    self.joinView = [self setupJoinView];
//    [self addSubview:self.joinView];
    
    UILabel * label = [UILabel new];
    
    label.text = @"暂无人参与....";
    label.textColor = [UIColor lightGrayColor];
    label.font = [UIFont systemFontOfSize:18 * SCREENH6];
    label.hidden = YES;
    
    self.noDataLabel = label;
    
    [self addSubview:self.noDataLabel];
    //1.初始化layout
    self.layout = [[UICollectionViewFlowLayout alloc] init];
    //设置collectionView滚动方向
    [self.layout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
    self.layout.itemSize =CGSizeMake(40 * SCREENH6, 45 * SCREENH6);
    self.layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    //2.初始化collectionView
    UICollectionView * collectionView = [[UICollectionView alloc] initWithFrame:self.bounds collectionViewLayout:self.layout];
    
    collectionView.backgroundColor = [UIColor clearColor];
    collectionView.showsHorizontalScrollIndicator = NO;
    //3.注册collectionViewCell
    //注意，此处的ReuseIdentifier 必须和 cellForItemAtIndexPath 方法中 一致 均为 cellId
    [collectionView registerClass:[AlreadyCollectionViewCell class] forCellWithReuseIdentifier:@"AlreadyCollectionViewCell"];
    //4.设置代理
    collectionView.delegate = self;
    collectionView.dataSource = self;
    //    [collectionView setContentOffset:CGPointMake(-30 * SCREENH6, 0)];
    
    self.collectionView = collectionView;
    
    [self addSubview:self.collectionView];
}

#pragma mark collectionView代理方法

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    
    return self.userArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    AlreadyCollectionViewCell *cell = (AlreadyCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"AlreadyCollectionViewCell" forIndexPath:indexPath];
    //设置数据
    NSDictionary * dict = self.userArray[indexPath.row];
    
    if([dict[@"head_img"] containsString:@"http"]) {

        NSURL * url = [NSURL URLWithString:dict[@"head_img"]];
        
        [cell.imageView sd_setImageWithURL:url];
    } else {
        
        NSString * usrlStr = [NSString stringWithFormat:@"%@%@",BASEURLSTR,dict[@"head_img"]];
        NSURL * url = [NSURL URLWithString:usrlStr];
        
        [cell.imageView sd_setImageWithURL:url];
    }
    
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    NSDictionary * dict = self.userArray[indexPath.row];
    
    NSString * ID = dict[@"goods_order_user_id"];
    
    if (self.goodsDetailsHeaderViewDelegate && [self.goodsDetailsHeaderViewDelegate respondsToSelector:@selector(clickUserImageButton:)])
    {
        [self.goodsDetailsHeaderViewDelegate clickUserImageButton:ID];
    }
}

- (void)layoutSubviews{
    
    [self.shopLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.banner.mas_bottom).offset(10 * SCREENH6);
        make.leading.mas_equalTo(self).offset(15 * SCREENH6);
        make.width.mas_equalTo(280 * SCREENH6);
    }];
    
    [self.distanceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.shopLabel);
        make.trailing.mas_equalTo(self).offset(-15 * SCREENH6);
    }];
    
    [self.commentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.starView.mas_trailing).offset(5 * SCREENH6);
        make.centerY.mas_equalTo(self.starView);
    }];
    
    [self.msgButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.mas_equalTo(40 * SCREENH6);
        make.top.mas_equalTo(self.starView.mas_bottom).offset(5 * SCREENH6);
        make.trailing.mas_equalTo(self).offset(-15 * SCREENH6);
    }];
    
    [self.callButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.centerY.mas_equalTo(self.msgButton);
        make.trailing.mas_equalTo(self.msgButton.mas_leading).offset(-20 * SCREENH6);
    }];
    
    [self.timeImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.starView);
        make.top.mas_equalTo(self.starView.mas_bottom).offset(10 * SCREENH6);
        make.width.height.mas_equalTo(14 * SCREENH6);
    }];
    
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.timeImageView);
        make.leading.mas_equalTo(self.timeImageView.mas_trailing).offset(5 * SCREENH6);
        make.trailing.mas_equalTo(self.callButton.mas_leading).offset(-5 * SCREENH6);
    }];
    
    [self.localImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.timeImageView);
        make.top.mas_equalTo(self.timeLabel.mas_bottom).offset(5 * SCREENH6);
    }];
    
    [self.localLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.localImageView);
        make.leading.mas_equalTo(self.timeLabel);
        make.trailing.mas_equalTo(self.callButton.mas_leading).offset(-5 * SCREENH6);
    }];
    
    [self.oneLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.localImageView);
        make.trailing.mas_equalTo(self);
        make.height.mas_equalTo(1 * SCREENH6);
        make.top.mas_equalTo(self.localLabel.mas_bottom).offset(7 * SCREENH6);
    }];
    
    [self.couponImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.oneLineView);
        make.top.mas_equalTo(self.oneLineView.mas_bottom).offset(15 * SCREENH6);
        make.width.mas_equalTo(25 * SCREENH6);
        make.height.mas_equalTo(15 * SCREENH6);
    }];
    
    [self.pointsLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.couponImageView);
        make.leading.mas_equalTo(self.couponImageView.mas_trailing).offset(5 * SCREENH6);
    }];
    
//    [self.redLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.centerY.mas_equalTo(self.pointsLabel);
//        make.leading.mas_equalTo(self.pointsLabel.mas_trailing).offset(5 * SCREENH6);
//    }];
    
    [self.pointsDetailsButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.pointsLabel);
        make.trailing.mas_equalTo(self).offset(-15 * SCREENH6);
    }];
    
    [self.twoLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.height.mas_equalTo(self.oneLineView);
        make.top.mas_equalTo(self.couponImageView.mas_bottom).offset(15 * SCREENH6);
    }];
    
    [self.validDateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.couponImageView);
        make.top.mas_equalTo(self.twoLineView.mas_bottom).offset(12 * SCREENH6);
    }];
    
    [self.threeLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.height.mas_equalTo(self.twoLineView);
        make.top.mas_equalTo(self.validDateLabel.mas_bottom).offset(12 * SCREENH6);
    }];
    
    [self.joinImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.validDateLabel);
        make.top.mas_equalTo(self.threeLineView.mas_bottom).offset(12 * SCREENH6);
        make.height.width.mas_equalTo(15 * SCREENH6);
    }];
    
    [self.joinLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.joinImageView);
        make.leading.mas_equalTo(self.joinImageView.mas_trailing).offset(5 * SCREENH6);
    }];
    
    [self.joinedDetailsButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(self.pointsDetailsButton);
        make.centerY.mas_equalTo(self.joinLabel);
    }];
    
    [self.noDataLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.joinImageView);
        make.top.mas_equalTo(self.joinImageView.mas_bottom).offset(15 * SCREENH6);
    }];

    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.joinImageView);
        make.top.mas_equalTo(self.joinImageView.mas_bottom).offset(7 * SCREENH6);
        make.height.mas_equalTo(45 * SCREENH6);
        make.trailing.mas_equalTo(self.threeLineView);
    }];
    
    [self.fourLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.height.mas_equalTo(self.threeLineView);
        make.top.mas_equalTo(self.joinImageView.mas_bottom).offset(57 * SCREENH6);
    }];
    
    [self.commmitImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.joinImageView);
        make.top.mas_equalTo(self.fourLineView.mas_bottom).offset(12 * SCREENH6);
        make.width.height.mas_equalTo(15 * SCREENH6);
    }];

    [self.titleCommitLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.commmitImageView);
        make.leading.mas_equalTo(self.commmitImageView.mas_trailing).offset(5 * SCREENH6);
    }];

    [self.commitDetailsButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(self.pointsDetailsButton);
        make.centerY.mas_equalTo(self.titleCommitLabel);
    }];
}

- (void)setupBanner
{
    // 初始化
    self.banner = [[ZYBannerView alloc] init];
    self.banner.dataSource = self;
    self.banner.delegate = self;
    self.banner.shouldLoop = YES;
    self.banner.autoScroll = YES;
    //    self.banner.pageControlFrame = CGRectMake(200, 195, 45, 32);
    
    [self addSubview:self.banner];
    
    // 设置frame
    self.banner.frame = CGRectMake(0,0,kScreenW,240 * SCREENH6);
}

#pragma mark - ZYBannerViewDataSource
// 返回 Banner 需要显示 Item(View) 的个数
- (NSInteger)numberOfItemsInBanner:(ZYBannerView *)banner
{
    return self.dataArray.count;
}

// 返回 Banner 在不同的 index 所要显示的 View (可以是完全自定义的v iew, 且无需设置 frame)
- (UIView *)banner:(ZYBannerView *)banner viewForItemAtIndex:(NSInteger)index
{
    
    NSString * imageName = self.dataArray[index];
    NSString * usrlStr = [NSString stringWithFormat:@"%@%@",BASEURLSTR,imageName];
    NSURL * url = [NSURL URLWithString:usrlStr];
    
    
    // 创建将要显示控件
    UIImageView *imageView = [[UIImageView alloc] init];
    //    imageView.image = [UIImage imageNamed:imageName];
    [imageView sd_setImageWithURL:url];
    //    [imageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"轮播1"]];
    imageView.contentMode = UIViewContentModeScaleAspectFill;
    
    return imageView;
}

- (UIView *)setupJoinView{
    
    UIView * view = [UIView new];
//
//    view.backgroundColor = ZRWhiteColor;
//
//    int startX = 5 * SCREENH6;
//    int startY = 5 * SCREENH6;
//    int widthSpace = 5 * SCREENH6;
//    int width = 35 * SCREENH6;
//    int height = 35 * SCREENH6;
//
//    NSLog(@"userArray===%@",self.userArray);
//
//    for (int i = 0 ; i < 6; i++) {
////        NSInteger index = i % 3;
////        NSInteger page = i / 3;
//
////        NSDictionary * dict = self.userArray[i];
//
////        NSString * usrlStr = [NSString stringWithFormat:@"%@%@",BASEURLSTR,dict[@"head_img"]];
////        NSURL * url = [NSURL URLWithString:usrlStr];
////
//        // 圆角按钮
//        UIButton * iconBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//        iconBtn.tag = i;//这句话不写等于废了
//        iconBtn.frame = CGRectMake(i * (width + widthSpace) + startX, startY, width, height);
//        [iconBtn setBackgroundColor:ZRBlackColor];
//        iconBtn.layer.cornerRadius = 17.5 * SCREENH6;
//        iconBtn.layer.masksToBounds = YES;
////        [iconBtn sd_setImageWithURL:url forState:UIControlStateNormal];
//
//        //按钮点击方法
//        [iconBtn addTarget:self action:@selector(iconBtnClick:) forControlEvents:UIControlEventTouchUpInside];
//
//        [view addSubview:iconBtn];
//    }
    
//    [view layoutIfNeeded];
    
    
    
    
    return view;
}

- (UIButton *)setupMoreButtonWithTitle:(NSString *)title{
    
    UIButton * button = [UIButton new];
    
    [button setTitle:title forState:UIControlStateNormal];
    [button setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(clickDetailButton:) forControlEvents:UIControlEventTouchUpInside];
    button.titleLabel.font = [UIFont systemFontOfSize:15 * SCREENH6];
    
    return button;
}

- (UILabel *)setupLabelWithTitle:(NSString *)title{
    
    UILabel * label = [UILabel new];
    
    label.text = title;
    label.textColor = ZRBlackColor;
    label.font = [UIFont systemFontOfSize:15];
    
    return label;
}

- (UIImageView *)setupImageViewImageWithImage:(NSString *)image{
    
    UIImageView * imageView = [UIImageView new];
    
    imageView.image = [UIImage imageNamed:image];
    
    return imageView;
}


- (UIButton *)setupButtonWithImage:(NSString *)image{
    
    UIButton * button = [UIButton new];
    
    [button addTarget:self action:@selector(cilckCallButtonOrMesButton:) forControlEvents:UIControlEventTouchUpInside];
    [button setBackgroundColor:ZRBlackColor];
    [button setImage:[UIImage imageNamed:image] forState:UIControlStateNormal];
    button.layer.cornerRadius = 20 * SCREENH6;
    button.layer.masksToBounds = YES;
    
    return button;
}

- (UIView *)setupLineView{
    
    UIView * lineView = [UIView new];
    
    lineView.backgroundColor = ColorWithRGB(0xDBE1E7);
    
    return lineView;
}


-(double)distanceBetweenOrderBy:(double) lat1 :(double) lat2 :(double) lng1 :(double) lng2{
    
    CLLocation *curLocation = [[CLLocation alloc] initWithLatitude:lat1 longitude:lng1];
    
    CLLocation *otherLocation = [[CLLocation alloc] initWithLatitude:lat2 longitude:lng2];
    
    double  distance  = [curLocation distanceFromLocation:otherLocation];
    
    return  distance;
    
}

- (void)clickDetailButton:(UIButton *)button{
    
    NSString * type;
    
    if (button == self.joinedDetailsButton) {
        
        type = @"join";
    }else if (button == self.commitDetailsButton){
        
        type = @"commit";
    }else if (button == self.pointsDetailsButton){
        
        type = @"coupon";
    }
    
    if (self.goodsDetailsHeaderViewDelegate && [self.goodsDetailsHeaderViewDelegate respondsToSelector:@selector(clickMoreButton:)])
    {
        [self.goodsDetailsHeaderViewDelegate clickMoreButton:type];
    }
}


- (void)tapAction{
   
    if (self.goodsDetailsHeaderViewDelegate && [self.goodsDetailsHeaderViewDelegate respondsToSelector:@selector(pushToMapViewConteoller)])
    {
        [self.goodsDetailsHeaderViewDelegate pushToMapViewConteoller];
    }
}


- (void)cilckCallButtonOrMesButton:(UIButton *)button{
    
    if (button == self.callButton) {
        
        NSMutableString* str=[[NSMutableString alloc] initWithFormat:@"tel:%@",self.dict[@"merchants_phone"]];
        
        UIWebView * callWebview = [[UIWebView alloc] init];
        
        [callWebview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:str]]];
        
        [self addSubview:callWebview];
    }else{
        
        NSString * user_id = DEF_GET_OBJECT(@"id");
        
        if ([user_id isEqualToString:self.merchants_user_id]) {
            
            
            [[NSNotificationCenter defaultCenter] postNotificationName:@"showAlertView" object:nil];            
        }else{
            
          [[NSNotificationCenter defaultCenter] postNotificationName:@"PushToChat" object:nil];
        }
    }
    
}


- (void)iconBtnClick:(UIButton *)button{
    
    
}

#pragma mark - TableView 占位图
- (NSString *)xy_noDataViewMessage {
    
    return @"";
}

- (UIColor *)xy_noDataViewMessageColor {
    
    return [UIColor blackColor];
}

- (CGFloat)cellHeightWithMsg:(NSString *)msg WithLabel:(UILabel *)label
{
    
    CGSize size = [label sizeThatFits:CGSizeMake(label.width, CGFLOAT_MAX)];
    return size.height;
}



@end
